package com.ccptmvp.framentmvp;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
 * @author dengpp
 */

public abstract class BaseFragment<P extends FramentIPresenter> extends Fragment implements FramentIView {
    // Presenter对象
    protected P MvpPre;

    private View inflate;

    private Unbinder bind;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //保持竖屏
        //保持竖屏
        requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        MvpPre = bindPresenter();


        //子类不再需要设置布局ID，也不再需要使用ButterKnife.bind()
        inflate = inflater.inflate(provideContentViewId(), container, false);
        bind = ButterKnife.bind(this, inflate);
        initView(inflate);
        return inflate;
    }

    // 绑定Presenter
    protected abstract P bindPresenter();

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    //ui及监听初始化操作
    protected abstract void initView(View root);

    @Override
    public Activity getSelfActivity() {
        return requireActivity();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        /**
         * 在生命周期结束时，将presenter与view之间的联系断开，防止出现内存泄露
         */
        if (MvpPre != null) {
            MvpPre.detachView();
        }
        if (bind != null) {
            bind.unbind();
        }
    }
}
